<%--
     商品编辑页面
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
<html>
<head>
    <title>Title</title>
    <script src="../../js/jquery-3.3.1.min.js" charset="utf-8"></script>
    <script src="../../js/cookie/jquery.cookie.js" charset="utf-8"></script>
    <script src="../../layui-v2.2.5/layui/layui.js" charset="utf-8"></script>
    <link rel="stylesheet" href="../../layui-v2.2.5/layui/css/layui.css" media="all">
</head>
<body>
<form class="layui-form">
    <input name="id" value="${record.id}" type="hidden">
    <input name="shareImgPath" id="shareImgPath"  type="hidden">
    <%--图片数据--%>
    <div class="layui-form-item">
        <label class="layui-form-label">游戏名称名称</label>
        <div class="layui-input-block">
            <input type="text" name="name" required  lay-verify="required" maxlength="20"
                   placeholder="请输入名称" autocomplete="off" class="layui-input" value="${record.name}">
        </div>
    </div>


    <div class="layui-form-item">
        <label class="layui-form-label">每日参与次数</label>
        <div class="layui-input-block">
            <input type="text" name="dayNumber" required  lay-verify="required"
                   placeholder="请输入每日可参与次数" autocomplete="off" class="layui-input" value="${record.dayNumber}" onkeyup="this.value=this.value.toString().match(/^\d+(?:\.\d{0,2})?/)">
        </div>
    </div>




    <div class="layui-form-item">
        <label class="layui-form-label">核销类型</label>
        <div class="layui-input-block">
            <select name="type" lay-verify="" lay-search>
                <option value="0" <c:if test="${record.type eq 0}">selected="selected"</c:if> >淘口令</option>
                <option value="1" <c:if test="${record.type eq 1}">selected="selected"</c:if> >二维码</option>
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">分享标题</label>
        <div class="layui-input-block">
            <input type="text" name="shareTitle" required  lay-verify="required"
                   placeholder="请输入分享标题" autocomplete="off" class="layui-input" value="${record.shareTitle}" >
        </div>
    </div>

    <div class="layui-form-item" style="margin-left: 7rem" >
        <div class="layui-upload">
            <button type="button" class="layui-btn layui-btn-normal" id="test1">分享图片</button>
            <div class="layui-upload-list">
                <img class="layui-upload-img"
                     src="${record.shareImgPath}"
                     id="demo1" style="height: 6rem;width: 6rem">
                <p id="demoText"></p>
            </div>
        </div>
    </div>

    <script>


        layui.use('upload', function(){
            var $ = layui.jquery
                ,upload = layui.upload;
            //普通图片上传
            var uploadInst = upload.render({
                elem: '#test1'
                ,url: '/shop/commodityShareImgUpload?commodityId=${record.id}'
                ,before: function(obj){
                    //预读本地文件示例，不支持ie8
                    obj.preview(function(index, file, result){
                        $('#demo1').attr('src', result); //图片链接（base64）

                    });
                }
                ,done: function(res){
                    //如果上传失败
                    if(res.code != 0){
                        return layer.msg('上传失败');
                    }
                    //        $("#logo").val(res.data.src);
                    layer.msg('上传成功');
                    $("#shareImgPath").val(res.data.src);
                    //上传成功
                }
                ,error: function(){
                    //演示失败状态，并实现重传
                    var demoText = $('#demoText');
                    demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                    demoText.find('.demo-reload').on('click', function(){
                        uploadInst.upload();
                    });
                }
            });

        });

    </script>


    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
            <%--<button type="reset" class="layui-btn layui-btn-primary">重置</button>--%>
        </div>
    </div>
</form>

<script>
    //Demo
    layui.use('form', function(){
        var form = layui.form;
        //监听提交
        form.on('submit(formDemo)', function(data){
            //layer.msg(JSON.stringify(data.field));
            $.ajax({
                url:'/shop/upMemberInfoController',
                type:'POST', //GET
                async:true,    //或false,是否异步
                data:data.field,
                timeout:5000,    //超时时间
                dataType:'json',    //返回的数据格式：json/xml/html/script/jsonp/text
                beforeSend:function(xhr){
                    /*console.log(xhr)
                     console.log('发送前')*/
                },
                success:function(data){
                    console.log(data);
                    if (data.code=='000000'){
                        layer.msg(data.msg,{time:1000,icon: 6},function () {
                            var index = parent.layer.getFrameIndex(window.name);
                            //关闭当前frame
                            parent.layer.close(index);
                        });
                        // 获得frame索引

                    }else {
                        layer.msg(data.msg,{time:1000,icon: 5});
                    }
                    //console.log(data)
                },
                error:function(xhr,textStatus){
                    console.log('错误')
                    console.log(xhr)
                    console.log(textStatus)
                },
                complete:function(){
                    //console.log('结束')
                }
            });
            return false;
        });
    });
</script>



</body>
</html>
